﻿using System;
using System.Collections.Generic;
using doufu.Common;
using doufu.Model;

namespace doufu.Service
{
    public class AboutService
    {
        public About Details(int id)
        {
            return CommonDac.Query(db => db.SingleOrDefault<About>(id));
        }

        public bool SaveAbout(About entity, IUser user)
        {
            return CommonDac.Query(
                  db =>
                  {
                      var count = db.ExecuteScalar<int>(
                           "select count(*) from About where about_name=@0 and about_id<>@1", entity.about_name, entity.about_id);
                      if (count == 0)
                      {
                          if (entity.about_id == 0)
                          {
                              entity.created_at = DateTime.Now;
                              entity.created_by = user.user.UserId;
                              db.Insert(entity);
                          }
                          else
                          {
                              db.Update(entity, new List<string> { "about_name", "about_content" });
                          }
                          return true;
                      }
                      return false;
                  });
        }
    }
}